/**
 * current 未结记账
 */
Jet().$package('app.current', function(J){
	var $=J.dom.id,
		$D=J.dom,
		$E=J.event,
		$H=J.http,
		$U=J.ui,
		$tool=J.tool;
	this.checkout_id=0;
	
	//未结记账列表
	this.listCurrent = function(page){		
		$D.html($('data_current'), '<tr><td colspan="5"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=BillProxy&f=current&limit=20&page='+page+'&group_id='+$('sel_group').value;
		$tool.get(uri,function(response){
			if(response.result==0){
				$tool.alertMsg(response.data);
				$D.empty($('data_current'));
				return
			}
			var data = response.data,
				user_id = data.user_id,
				$appCurrent = app.current;			
			if($appCurrent.checkout_id != data.checkout_id){
				$appCurrent.checkout_id = data.checkout_id;
				$appCurrent.mySummary();	//汇总				
			}
			//列表
			var memberKV = app.memberKV,
				sHtml = '';
			J.each(data.list,function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+$tool.datetime(row.b_create_time)+'</td>';
				sHtml += '<td>'+row.b_note+'</td>';
				sHtml += '<td>'+row.b_amount+'</td>';
				sHtml += '<td>'+memberKV[row.b_creator]+'</td>';
				var operLink = '<a href="javascript:app.current.detail('+row.b_id+');">详细</a>';
				if(user_id==row.b_creator){
					var sOnclick = 'app.current.removeBill('+row.b_id+',this);';
					operLink += '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="'+sOnclick+'">删除</a>';
				}
				sHtml += '<td>'+operLink+'</td>';
				sHtml += '</tr>';
			});
			if(sHtml!='') $D.html($('data_current'), sHtml);
			else $D.html($('data_current'), '<tr><td colspan="5">暂无数据~</td></tr>');
			$U.pager({ 'pageNode': $('page_current'), 
				'pageCurrent': page, 
				'pageCount': 20, 
				'count': data.count, 
				'callback': app.current.listCurrent 
			});				
		});		
	};
	
	//个人汇总
	this.mySummary = function() {
		var uri = '/ajax.php?d=bill&c=BillProxy&f=mysummary&checkout_id='+this.checkout_id;
		$tool.get(uri,function(response){
			if(response.result==1) {
				var oSummary = response.data;
				if(oSummary != false){
					$D.text($('my_summary_pay'), oSummary.cu_person_pay);
					$D.text($('my_summary_spend'), oSummary.cu_person_spend);
					$D.text($('my_summary_balance'), oSummary.cu_balance);
				}else{
					$D.text($('my_summary_pay'), '0.00');
					$D.text($('my_summary_spend'), '0.00');
					$D.text($('my_summary_balance'), '0.00');
				}
			}
		});
	};
	
	//记账详细
	this.detail = function(b_id){
		var oDialog = J.ui.dialog({ 'divNode':$('bill_items'), 'title':'记账详细',	'width':'420px', 'height':'auto' });
		$D.html($('bill_items_data'), '<tr><td colspan="4"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=BillProxy&f=detail&bill_id='+b_id;
		$tool.get(uri,function(response){
			var memberKV = app.memberKV;
			var sHtml = '';
			J.each(response.data.billItems,function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+memberKV[row.u_id]+'</td>';
				sHtml += '<td>'+row.bi_pay+'</td>';
				sHtml += '<td>'+row.bi_consume+'</td>';
				sHtml += '<td>'+Number(row.bi_pay-row.bi_consume).toFixed(2)+'</td>';
				sHtml += '</tr>';
			});
			$D.html($('bill_items_data'), sHtml);
			var oBill = response.data.bill;
			var aField = $D.mini('#bill_items dd');
			$D.text(aField[0], memberKV[oBill.b_creator]);
			$D.text(aField[1], oBill.b_note);
			oDialog.locating();
		});		
	};
		
	//记账
	this.billing = function() {
		var sHtml = '';
		J.each(app.memberKV, function(id, name){
			sHtml += '<tr id="user'+id+'">';
			sHtml += '<td>'+name+'<input type="hidden" name="users[]" value="'+id+'" /></td>';
			sHtml += '<td><input type="text" name="pay'+id+'" value="" size="14" class="search-text" /></td>';
			sHtml += '<td><input type="text" name="consume'+id+'" value="" size="14" class="search-text" /></td>';
			sHtml += '<td><a href="javascript:app.current.delBillRow('+id+');">移除</a></td>';
			sHtml += '</tr>';
			$D.html($('billing_user_data'), sHtml);
		});
		$tool.activeButton('btn_bill_submit');
		this.resetBillForm();
		J.ui.dialog({
			'divNode':$('billing_dialog'),
			'title':'记账',
			'width':'500px',
			'height':'auto',
			'isDrag':false,
			'isLayer':true
		});
	};
	
	//提交记账
	this.addBill = function() {
		var context = this;
		$tool.disableButton('btn_bill_submit');
		this.resetBillForm();
		var dBillingForm = $('billing_form');
		dBillingForm['checkout_id'].value = this.checkout_id;
		var uri='/ajax.php?d=bill&c=BillProxy&f=billing';
		$tool.post(uri,$H.serialize(dBillingForm),function(response){
			if(response.result==0) {
				var errorData = response.data,
					sError = '';
				if(typeof(errorData)==='object'){
					for(k in errorData){
						$D.setStyle(dBillingForm[k],'backgroundColor','#FEE');
						sError = errorData[k];
					}
				}else sError = errorData;
				var dBillingError = $D.mini('#billing_form div.bill_form_error')[0];
				$D.text(dBillingError, sError);				
				$D.show(dBillingError, 'inline-block');
			}else{
				var memberKV = app.memberKV,
					row = response.data,
					sHtml = '';
				sHtml += '<tr>';
				sHtml += '<td>'+$tool.datetime(row.b_create_time)+'</td>';
				sHtml += '<td>'+row.b_note+'</td>';
				sHtml += '<td>'+row.b_amount+'</td>';
				sHtml += '<td>'+memberKV[row.b_creator]+'</td>';
				var operLink = '<a href="javascript:app.current.detail('+row.b_id+');">详细</a>';
				var sOnclick = 'app.current.removeBill('+row.b_id+',this);';
				operLink += '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="'+sOnclick+'">删除</a>';
				sHtml += '<td>'+operLink+'</td>';
				sHtml += '</tr>';
				$D.prepend($('data_current'), sHtml);
				context.mySummary();	//汇总
				$U.closeDialog('billing_dialog');
				$tool.showTip('记账成功');
			}
			$tool.activeButton('btn_bill_submit');
		});
	};
	
	//重置记账表单
	this.resetBillForm = function(){
		var dBillingError = $D.mini('#billing_form div.bill_form_error')[0];
		$D.hide(dBillingError);
		var aInputNode = $D.mini('#billing_form input[type="text"]');
		for(var i=0;i<aInputNode.length;i++){
			$D.setStyle(aInputNode[i],'backgroundColor','');
		}
	};
	
	//移除记账表单行
	this.delBillRow = function(user_id) {
		$D.remove($('user'+user_id));
	};
	
	//结账
	this.checkout = function() {
		var oDialog = J.ui.dialog({
			'divNode':$('co_confirm_dialog'), 
			'title':'账单汇总', 
			'width':'600px',
			'height':'auto',
			'isDrag':false,
			'isLayer':true
		});
		$tool.activeButton('btn_close_bill');
		$D.html($('user_checkout_data'), '<tr><td colspan="4"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=CheckoutProxy&f=summary&checkout_id='+this.checkout_id;
		$tool.get(uri,function(response){
			if(response.result==0){
				$tool.alertMsg(response.data);
				$D.empty($('user_checkout_data'));
				return;
			}
			var memberKV = app.memberKV,
				checkoutRow = response.data.checkout,
				coUserList = response.data.co_users,
				sHtml = '';
			J.each(coUserList, function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+memberKV[row.u_id]+'</td>';
				sHtml += '<td>'+row.cu_person_pay+'</td>';
				sHtml += '<td>'+row.cu_person_spend+'</td>';
				sHtml += '<td>'+row.cu_balance+'</td>';
				sHtml += '</tr>';
			});
			sHtml += '<tr><td>合计：</td><td colspan="3">'+checkoutRow.c_total+'</td></tr>';
			$D.html($('user_checkout_data'), sHtml);
			oDialog.locating();
		});		
		$D.hide($D.mini('#co_confirm_dialog div.form-error')[0]);
	};
	
	//确定结账
	this.sureCheckout = function() {
		var context = this;
		J.tool.confirm('您确定要结束账单吗？', function(){
			$tool.disableButton('btn_close_bill');
			var uri='/ajax.php?d=bill&c=CheckoutProxy&f=checkout';
			$tool.post(uri,{checkout_id:context.checkout_id},function(response){
				if(response.result==0) {				
					var dError = $D.mini('#co_confirm_dialog div.form-error')[0];
					$D.text(dError, response.data);				
					$D.show(dError);
				}else{
					$D.html($('data_current'), '<tr><td colspan="5">暂无数据~</td></tr>');
					$D.text($('my_summary_pay'), '0.00');
					$D.text($('my_summary_spend'), '0.00');
					$D.text($('my_summary_balance'), '0.00');
					context.checkout_id = response.data.c_id;
					$U.closeDialog('co_confirm_dialog');
					$tool.showTip('结束账单成功');
				}
				$tool.activeButton('btn_close_bill');
			});
		});
	};	
	
	//删除群组
	this.removeBill = function(b_id,aNode){
		var context = this;
		J.tool.confirm('您确定删除吗？', function(){
			var tdNode = aNode.parentNode;
			var ahtml = tdNode.innerHTML;
			$D.html(tdNode, '<div class="loading"></div>');
			var uri = '/ajax.php?d=bill&c=BillProxy&f=remove';
			$tool.post(uri,{bill_id:b_id},function(response){
				if(response.result==0) {
					$tool.alertMsg(response.data);
					$D.html(tdNode, ahtml);
				} else {
					context.mySummary();
					$D.remove(tdNode.parentNode);
				}
			});
		});		
	};
	
	
});